package com.guoliang.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guoliang.eduService.entity.EduCourse;
import com.guoliang.eduService.entity.EduCourseDescription;
import com.guoliang.eduService.entity.vo.CourseFrontVo;
import com.guoliang.eduService.entity.vo.CourseInfoVo;
import com.guoliang.eduService.entity.vo.CoursePublishVo;
import com.guoliang.eduService.entity.vo.CourseQuery;
import com.guoliang.eduService.mapper.EduCourseMapper;
import com.guoliang.eduService.service.EduChapterService;
import com.guoliang.eduService.service.EduCourseDescriptionService;
import com.guoliang.eduService.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guoliang.eduService.service.EduVideoService;
import com.guoliang.service.exceptionHandler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author leo
 * @since 2023-01-18
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
      //向课程表添加课程基本信息
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert<=0) {
            throw new MyException(20001,"添加课程信息失败");
        }
        //向课程简介表添加信息
        String id=eduCourse.getId();
        System.out.println(id);
        EduCourseDescription eduCourseDescription=new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescriptionService.save(eduCourseDescription);
        return id;
    }

    @Override
    public CourseInfoVo getByCourseInfoVoById(String id) {
        CourseInfoVo courseInfoVo=new CourseInfoVo();
        //先查询课程表
        EduCourse eduCourse = baseMapper.selectById(id);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //再查询课程描述表
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(id);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        //封装返回
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfo) {
        //修改EduCourse
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfo,eduCourse);
        baseMapper.updateById(eduCourse);
        //修改描述表
        EduCourseDescription eduCourseDescription=new EduCourseDescription();
        BeanUtils.copyProperties(courseInfo,eduCourseDescription);
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public CoursePublishVo getCoursePublish(String id) {
        return baseMapper.getPublishCourseInfo(id);
    }

    @Override
    public Page<EduCourse> pageCourseCondition(CourseQuery courseQuery, Integer page, Integer limit) {
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getTitle())) wrapper.like("title",courseQuery.getTitle());
        if (!StringUtils.isEmpty(courseQuery.getTeacherId())) wrapper.eq("teacher_id",courseQuery.getTeacherId());
        if (!StringUtils.isEmpty(courseQuery.getSubjectId())) wrapper.eq("subject_id",courseQuery.getSubjectId());
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) wrapper.eq("subject_parent_id",courseQuery.getSubjectParentId());
        Page<EduCourse> coursePage=new Page<>(page,limit);
         return baseMapper.selectPage(coursePage, wrapper);
    }

    @Override
    public void removeCourse(String id) {
        //删除小节
        eduVideoService.removeVideoByCourseId(id);
        //删除章节
        eduChapterService.removeChapterByCourseId(id);
        //删除课程描述
        eduCourseDescriptionService.removeById(id);
        //删除课程
        int result=baseMapper.deleteById(id);
       if (result==0){
           throw new MyException(20001,"删除失败");
       }
    }

    @Override
    public Map<String, Object> getFrontCourseList(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getTitle()))
            wrapper.like("title",courseQuery.getTitle());
        if (!StringUtils.isEmpty(courseQuery.getTeacherId()))
            wrapper.eq("teacher_id",courseQuery.getTeacherId());
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId()))
            wrapper.eq("subject_parent_id",courseQuery.getSubjectParentId());
        if (!StringUtils.isEmpty(courseQuery.getSubjectId()))
            wrapper.eq("subject_id",courseQuery.getSubjectId());
        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort()))
            wrapper.orderByDesc("buy_count");
        if (!StringUtils.isEmpty(courseQuery.getPriceSort()))
            wrapper.orderByDesc("price");
        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort()))
            wrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(pageParam, wrapper);
        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public CourseFrontVo getFrontCourseInfo(String id) {
        CourseFrontVo courseInfo = baseMapper.getFrontCourseInfo(id);
        return courseInfo;
    }

    @Override
    public void updateBuyCount(String id) {
        baseMapper.updateBuyCount(id);
    }

    @Override
    public void updateViewCount(String id, Long viewCount) {
        baseMapper.updateViewCount(id,viewCount);
    }
}
